package servlets.users;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.JsonManager;

import org.jsoup.Jsoup;

import structures.ADsList;
import structures.GamifyResult;
import structures.Seller;
import structures.User;
import twitter4j.internal.org.json.JSONArray;
import twitter4j.internal.org.json.JSONObject;

public class ShowUser extends HttpServlet {

	private static final long serialVersionUID = -3801859519441924766L;
	private static final String URLBASE = "http://localhost:8080/DatabaseRESTWebService/GetIdUserServlet?";
	private static final String GAMIFYURLBASE = "http://localhost:8080/GamifyRESTWebProject/GetUserBadgesNameAndUrl?";
	//private static final String GAMIFYURLBASE = "http://gamificationservice.herokuapp.com/GetUserBadgesNameAndUrl?";
	private static final String SELLERADSURLBASE = "http://localhost:8080/DatabaseRESTWebService/GetSellerADs?";
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		String idUser, answer, jsonResult;
		ArrayList<GamifyResult> gamifyResults;
		JSONArray array;
		URL url;
		Seller seller = null;
		ADsList adsBoughtsList, adsSoldList, adsAvailableList = null;
		JsonManager jsonManager;
		JSONObject answerJson;
		User user = null;
		jsonManager = new JsonManager();
		idUser = request.getParameter("idUser");
		gamifyResults = new ArrayList<GamifyResult>();
		
		try {
			//Take user infos
			url = new URL(URLBASE + "idUser=" + idUser);
			answer = Jsoup.connect(url.toString()).ignoreContentType(true).execute().body();
			if(answer.equals("null"))
				throw new Exception();
			user = jsonManager.toUser(answer);

			
			//Take user advertisements already sold
			url = new URL(SELLERADSURLBASE + "idSeller=" + idUser+ "&adType=Sold");
			answer = Jsoup.connect(url.toString()).ignoreContentType(true).execute().body();
			if(answer.equals("null"))
				throw new Exception();
			adsSoldList = jsonManager.toADsList(answer);
			

			//Take user advertisements still available
			url = new URL(SELLERADSURLBASE + "idSeller=" + idUser + "&adType=Available");
			answer = Jsoup.connect(url.toString()).ignoreContentType(true).execute().body();
			if(answer.equals("null"))
				throw new Exception();
			adsAvailableList = jsonManager.toADsList(answer);

			//Take advertisements bought by the user 
			url = new URL(SELLERADSURLBASE + "idSeller=" + idUser + "&adType=Bought");
			answer = Jsoup.connect(url.toString()).ignoreContentType(true).execute().body();
			if(answer.equals("null"))
				throw new Exception();
			adsBoughtsList = jsonManager.toADsList(answer);

			try{
				//Take user badges
				url = new URL(GAMIFYURLBASE + "idUser=" + idUser);
				answer = Jsoup.connect(url.toString()).ignoreContentType(true).execute().body();
				answerJson = new JSONObject(answer);
				if (answerJson.has("result")){
					array = answerJson.getJSONArray("result");
					for (int i = 0; i < array.length(); i++) {
			        	//Check if between all the annotations, some are related to places
			            gamifyResults.add(jsonManager.toGamifyResult(array.getJSONObject(i).toString()));
			        }
	            }
			}catch(Exception e){
				gamifyResults = new ArrayList<GamifyResult>();
			}
		
			//seller = new Seller(user, adsBoughtsList, adsSoldList, adsAvailableList, gamifyResults);
			seller = new Seller(user, adsBoughtsList, adsSoldList, adsAvailableList, gamifyResults);
			
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			jsonResult = jsonManager.generateJSON(seller);
			response.getWriter().write(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}


